<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.dom.annotate</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.dom');
  goog.require('goog.dom.annotate');
  goog.require('goog.testing.jsunit');
</script>
<style type="text/css">
  .c0 {background-color:#ff0}
  .c1 {background-color:#0ff}
</style>
</head>
<body>
<span id="p">Tom &amp; Jerry</span>
<table>
  <tr id="q">
    <td>This <b>little</b> piggy</td>
    <td class="s">That little <i>pig</i>gy</td>
  </tr>
  <tr id="r">
    <td>This <b>little</b> piggy</td>
    <td class="s">That little <i>pig</i>gy</td>
  </tr>
  <tr id="t">
    <td>This <b>little</b> piggy</td>
    <td class="s">That little <i>Pig</i>gy</td>
  </tr>
  <tr id="u">
    <td>This <b>little</b> piggy</td>
    <td class="s">That little <i>Pig</i>gy</td>
  </tr>
</table>

<div id="o">
<object classid="clsid:SAMPLE-UNRECOGNIZED-ID" width="100" height="50">
  <param name="BorderStyle" value="1" />
  <param name="MousePointer" value="0" />
  <param name="Enabled" value="1" />
  <param name="Min" value="0" />
  <param name="Max" value="10" />
  Your browser cannot display this object.
</object>
</div>

<script id="script">var variable;</script>
<style id="style" type="text/css">.orange{color:orange}</style>
<span id="comment"><!-- note --></span>

<script>
  var $ = goog.dom.getElement;

  var TEXT = 'This little piggy cried "Wee! Wee! Wee!" all the way home.';

  function doAnnotation(termIndex, termHtml) {
    return '<span class="c' + termIndex + '">' + termHtml + '</span>';
  }

  // goog.dom.annotate.annotateText tests

  function testAnnotateText() {
    var terms = [['pig', true]];
    var html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
    assertEquals(null, html);

    terms = [['pig', false]];
    html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
    assertEquals('This little <span class="c0">pig</span>gy cried ' +
                 '&quot;Wee! Wee! Wee!&quot; all the way home.', html);

    terms = [[' piggy ', true]];
    html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
    assertEquals(null, html);

    terms = [[' piggy ', false]];
    html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
    assertEquals('This little<span class="c0"> piggy </span>cried ' +
                 '&quot;Wee! Wee! Wee!&quot; all the way home.', html);

    terms = [['goose', true], ['piggy', true]];
    html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
    assertEquals('This little <span class="c1">piggy</span> cried ' +
                 '&quot;Wee! Wee! Wee!&quot; all the way home.', html);
  }

  function testAnnotateTextHtmlEscaping() {
    var terms = [['a', false]];
    var html = goog.dom.annotate.annotateText('&a', terms, doAnnotation)
    assertEquals('&amp;<span class="c0">a</span>', html);

    terms = [['a', false]];
    html = goog.dom.annotate.annotateText('a&', terms, doAnnotation)
    assertEquals('<span class="c0">a</span>&amp;', html);

    terms = [['&', false]];
    html = goog.dom.annotate.annotateText('&', terms, doAnnotation)
    assertEquals('<span class="c0">&amp;</span>', html);
  }

  function testAnnotateTextIgnoreCase() {
    var terms = [['wEe', true]];
    var html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation, true);
    assertEquals('This little piggy cried &quot;<span class="c0">Wee</span>! ' +
                 '<span class="c0">Wee</span>! <span class="c0">Wee</span>!' +
                 '&quot; all the way home.', html);

    terms = [['WEE!', true], ['HE', false]];
    html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation, true);
    assertEquals('This little piggy cried &quot;<span class="c0">Wee!</span> ' +
                 '<span class="c0">Wee!</span> <span class="c0">Wee!</span>' +
                 '&quot; all t<span class="c1">he</span> way home.', html);
  }

  function testAnnotateTextOverlappingTerms() {
    var terms = [['tt', false], ['little', false]];
    var html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
    assertEquals('This <span class="c1">little</span> piggy cried &quot;Wee! ' +
                 'Wee! Wee!&quot; all the way home.', html);
  }

  // goog.dom.annotate.annotateTerms tests

  function testAnnotateTerms() {
    var terms = [['pig', true]];
    assertFalse(goog.dom.annotate.annotateTerms($('p'), terms, doAnnotation));
    assertEquals('Tom &amp; Jerry', $('p').innerHTML);

    terms = [['Tom', true]];
    assertTrue(goog.dom.annotate.annotateTerms($('p'), terms, doAnnotation));
    var spans = goog.dom.getElementsByTagNameAndClass('SPAN', 'c0', $('p'));
    assertEquals(1, spans.length);
    assertEquals('Tom', spans[0].innerHTML);
    assertEquals(' & Jerry', spans[0].nextSibling.nodeValue);
  }

  function testAnnotateTermsInTable() {
    var terms = [['pig', false]];
    assertTrue(goog.dom.annotate.annotateTerms($('q'), terms, doAnnotation));
    var spans = goog.dom.getElementsByTagNameAndClass('SPAN', 'c0', $('q'));
    assertEquals(2, spans.length);
    assertEquals('pig', spans[0].innerHTML);
    assertEquals('gy', spans[0].nextSibling.nodeValue);
    assertEquals('pig', spans[1].innerHTML);
    assertEquals('I', spans[1].parentNode.tagName);
  }

  function testAnnotateTermsWithClassExclusions() {
    var terms = [['pig', false]];
    var classesToIgnore = ['s'];
    assertTrue(goog.dom.annotate.annotateTerms($('r'), terms, doAnnotation,
                                               false, classesToIgnore));
    var spans = goog.dom.getElementsByTagNameAndClass('SPAN', 'c0', $('r'));
    assertEquals(1, spans.length);
    assertEquals('pig', spans[0].innerHTML);
    assertEquals('gy', spans[0].nextSibling.nodeValue);
  }

  function testAnnotateTermsIgnoreCase() {
    var terms1 = [['pig', false]];
    assertTrue(goog.dom.annotate.annotateTerms(
        $('t'), terms1, doAnnotation, true));
    var spans = goog.dom.getElementsByTagNameAndClass('SPAN', 'c0', $('t'));
    assertEquals(2, spans.length);
    assertEquals('pig', spans[0].innerHTML);
    assertEquals('gy', spans[0].nextSibling.nodeValue);
    assertEquals('Pig', spans[1].innerHTML);

    var terms2 = [['Pig', false]];
    assertTrue(goog.dom.annotate.annotateTerms(
        $('u'), terms2, doAnnotation, true));
    var spans = goog.dom.getElementsByTagNameAndClass('SPAN', 'c0', $('u'));
    assertEquals(2, spans.length);
    assertEquals('pig', spans[0].innerHTML);
    assertEquals('gy', spans[0].nextSibling.nodeValue);
    assertEquals('Pig', spans[1].innerHTML);
  }

  function testAnnotateTermsInObject() {
    var terms = [['object', true]];
    assertTrue(goog.dom.annotate.annotateTerms($('o'), terms, doAnnotation));
    var spans = goog.dom.getElementsByTagNameAndClass('SPAN', 'c0', $('o'));
    assertEquals(1, spans.length);
    assertEquals('object', spans[0].innerHTML);
  }

  function testAnnotateTermsInScript() {
    var terms = [['variable', true]];
    assertFalse(goog.dom.annotate.annotateTerms($('script'), terms,
                                                doAnnotation));
  }

  function testAnnotateTermsInStyle() {
    var terms = [['color', true]];
    assertFalse(goog.dom.annotate.annotateTerms($('style'), terms,
                                                doAnnotation));
  }

  function testAnnotateTermsInHtmlComment() {
    var terms = [['note', true]];
    assertFalse(goog.dom.annotate.annotateTerms($('comment'), terms,
                                                doAnnotation));
  }

</script>
</body>
</html>
